﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace HalconDotNet
{
    // Token: 0x02000012 RID: 18
    [ToolboxBitmap(typeof(HWindowControl))]
    public class HSmartWindowControl : UserControl
    {
        /// <summary>
        ///   Occurs when the mouse is moved over the HALCON window. Note that
        ///   delta is meaningless here.
        /// </summary>
        // Token: 0x1400000E RID: 14
        // (add) Token: 0x06000097 RID: 151 RVA: 0x000040F8 File Offset: 0x000022F8
        // (remove) Token: 0x06000098 RID: 152 RVA: 0x00004130 File Offset: 0x00002330
        [Description("Occurs when the mouse is moved over the HALCON window. Note that delta is meaningless here. The position is returned in the image coordinate system.")]
        [Category("Mouse")]
        public event HMouseEventHandler HMouseMove;

        /// <summary>
        ///   Occurs when a button is pressed over the HALCON window. Note that
        ///   delta is meaningless here.
        /// </summary>
        // Token: 0x1400000F RID: 15
        // (add) Token: 0x06000099 RID: 153 RVA: 0x00004168 File Offset: 0x00002368
        // (remove) Token: 0x0600009A RID: 154 RVA: 0x000041A0 File Offset: 0x000023A0
        [Description("Occurs when a button is pressed over the HALCON window. Note that delta is meaningless here. The position is returned in the image coordinate system.")]
        [Category("Mouse")]
        public event HMouseEventHandler HMouseDown;

        /// <summary>
        ///   Occurs when a button is released over the HALCON window. Note that
        ///   delta is meaningless here.
        /// </summary>
        // Token: 0x14000010 RID: 16
        // (add) Token: 0x0600009B RID: 155 RVA: 0x000041D8 File Offset: 0x000023D8
        // (remove) Token: 0x0600009C RID: 156 RVA: 0x00004210 File Offset: 0x00002410
        [Description("Occurs when a button is released over the HALCON window. Note that delta is meaningless here. The position is returned in the image coordinate system.")]
        [Category("Mouse")]
        public event HMouseEventHandler HMouseUp;

        /// <summary>
        ///   Occurs when a button is double-clicked over a HALCON window. Note
        ///   that delta is meaningless here.
        /// </summary>
        // Token: 0x14000011 RID: 17
        // (add) Token: 0x0600009D RID: 157 RVA: 0x00004248 File Offset: 0x00002448
        // (remove) Token: 0x0600009E RID: 158 RVA: 0x00004280 File Offset: 0x00002480
        [Description("Occurs when a button is double-clicked over a HALCON window. Note that delta is meaningless here. The position is returned in the image coordinate system.")]
        [Category("Mouse")]
        public event HMouseEventHandler HMouseDoubleClick;

        /// <summary>
        ///   Occurs when the wheel is used over a HALCON window while it has
        ///   focus. Note that button is meaningless here.
        /// </summary>
        // Token: 0x14000012 RID: 18
        // (add) Token: 0x0600009F RID: 159 RVA: 0x000042B8 File Offset: 0x000024B8
        // (remove) Token: 0x060000A0 RID: 160 RVA: 0x000042F0 File Offset: 0x000024F0
        [Description("Occurs when the wheel is used over a HALCON window while it has focus. Note that button is meaningless here. The position is returned in the image coordinate system.")]
        [Category("Mouse")]
        public event HMouseEventHandler HMouseWheel;

        /// <summary>
        ///   Occurs after the HALCON window has been initialized
        /// </summary>
        // Token: 0x14000013 RID: 19
        // (add) Token: 0x060000A1 RID: 161 RVA: 0x00004328 File Offset: 0x00002528
        // (remove) Token: 0x060000A2 RID: 162 RVA: 0x00004360 File Offset: 0x00002560
        [Description("Occurs after the HALCON window has been initialized.")]
        [Category("Behavior")]
        public event HInitWindowEventHandler HInitWindow;

        /// <summary>
        /// Occurs when an internal error in the HSmartWindowControl happens.
        /// </summary>
        // Token: 0x14000014 RID: 20
        // (add) Token: 0x060000A3 RID: 163 RVA: 0x00004398 File Offset: 0x00002598
        // (remove) Token: 0x060000A4 RID: 164 RVA: 0x000043D0 File Offset: 0x000025D0
        public event HSmartWindowControl.HErrorHandler HErrorNotify;

        // Token: 0x1700001C RID: 28
        // (get) Token: 0x060000A5 RID: 165 RVA: 0x0000384D File Offset: 0x00001A4D
        protected override Size DefaultSize
        {
            get
            {
                return new Size(320, 240);
            }
        }

        // Token: 0x060000A6 RID: 166 RVA: 0x00004408 File Offset: 0x00002608
        public HSmartWindowControl()
        {
            this.InitializeComponent();
        }

        // Token: 0x1700001D RID: 29
        // (get) Token: 0x060000A7 RID: 167 RVA: 0x0000446C File Offset: 0x0000266C
        [Browsable(false)]
        public HWindow HalconWindow
        {
            get
            {
                if (this._hwindow == null && base.Width > 0 && base.Height > 0)
                {
                    this.CreateHWindow();
                }
                return this._hwindow;
            }
        }

        // Token: 0x1700001E RID: 30
        // (get) Token: 0x060000A8 RID: 168 RVA: 0x00004494 File Offset: 0x00002694
        [Browsable(false)]
        public IntPtr HalconID
        {
            get
            {
                if (this._hwindow != null)
                {
                    return this._hwindow.Handle;
                }
                return IntPtr.Zero;
            }
        }

        /// <summary>
        /// Reliable way to check whether we are in designer mode.
        /// </summary>
        // Token: 0x1700001F RID: 31
        // (get) Token: 0x060000A9 RID: 169 RVA: 0x000044B0 File Offset: 0x000026B0
        private static bool RunningInDesignerMode
        {
            get
            {
                bool flag = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
                if (!flag)
                {
                    using (Process currentProcess = Process.GetCurrentProcess())
                    {
                        string fileDescription = currentProcess.MainModule.FileVersionInfo.FileDescription;
                        if (fileDescription != null && fileDescription.ToLowerInvariant().Contains("microsoft visual studio"))
                        {
                            return true;
                        }
                        return currentProcess.ProcessName.ToLowerInvariant().Contains("devenv");
                    }
                    return flag;
                }
                return flag;
            }
        }

        /// <summary>
        ///   Adapt ImagePart to show the full image. If HKeepAspectRatio is on,
        ///   the contents of the HALCON window are rescaled while keeping the aspect
        ///   ratio. Otherwise, the HALCON window contents are rescaled to fill up
        ///   the HSmartWindowControl.
        /// </summary>
        /// <param name="reference"></param>
        // Token: 0x060000AA RID: 170 RVA: 0x00004530 File Offset: 0x00002730
        public void SetFullImagePart(HImage reference = null)
        {
            if (reference != null)
            {
                int num;
                int num2;
                reference.GetImageSize(out num, out num2);
                this._hwindow.SetPart(0, 0, num2 - 1, num - 1);
                return;
            }
            if (this.HKeepAspectRatio)
            {
                this._hwindow.SetPart(0, 0, -2, -2);
                return;
            }
            this._hwindow.SetPart(0, 0, -1, -1);
        }

        /// <summary>
        /// Force the Window Control to be repainted in a thread safe manner.
        /// </summary>
        /// <param name="context"></param>
        // Token: 0x060000AB RID: 171 RVA: 0x00004587 File Offset: 0x00002787
        private int HWindowCallback(IntPtr context)
        {
            if (base.InvokeRequired)
            {
                base.BeginInvoke(new MethodInvoker(delegate ()
                {
                    base.Invalidate();
                }));
            }
            else
            {
                base.Invalidate();
            }
            return 2;
        }

        /// <summary>
        /// Real pain in the ass. In WinForms events do not bubble, as it is the case
        /// for most frameworks.
        /// This means, we need to hand code the event chain.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        // Token: 0x060000AC RID: 172 RVA: 0x000045AD File Offset: 0x000027AD
        private void ctrl_Click(object sender, EventArgs e)
        {
            base.InvokeOnClick(this, EventArgs.Empty);
        }

        /// <summary>
        /// Redirect relevant events up to the containing Form.
        /// </summary>
        /// <param name="e">Reference to the containing Control.</param>
        // Token: 0x060000AD RID: 173 RVA: 0x000045BC File Offset: 0x000027BC
        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            if (!HSmartWindowControl.RunningInDesignerMode)
            {
                e.Control.Click += this.ctrl_Click;
                e.Control.GotFocus += this.Control_GotFocus;
                e.Control.LostFocus += this.Control_LostFocus;
                e.Control.MouseEnter += this.Control_MouseEnter;
                e.Control.MouseLeave += this.Control_MouseLeave;
                e.Control.MouseHover += this.Control_MouseHover;
                e.Control.SizeChanged += this.Control_SizeChanged;
                e.Control.KeyDown += this.Control_KeyDown;
                e.Control.KeyPress += this.Control_KeyPress;
                e.Control.KeyUp += this.Control_KeyUp;
                e.Control.Resize += this.Control_Resize;
            }
        }

        // Token: 0x060000AE RID: 174 RVA: 0x000046D7 File Offset: 0x000028D7
        private void Control_Resize(object sender, EventArgs e)
        {
            this.OnResize(e);
        }

        // Token: 0x060000AF RID: 175 RVA: 0x000046E0 File Offset: 0x000028E0
        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            this.OnKeyUp(e);
        }

        // Token: 0x060000B0 RID: 176 RVA: 0x000046E9 File Offset: 0x000028E9
        private void Control_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.OnKeyPress(e);
        }

        // Token: 0x060000B1 RID: 177 RVA: 0x000046F2 File Offset: 0x000028F2
        private void Control_KeyDown(object sender, KeyEventArgs e)
        {
            this.OnKeyDown(e);
        }

        // Token: 0x060000B2 RID: 178 RVA: 0x000046FB File Offset: 0x000028FB
        private void Control_SizeChanged(object sender, EventArgs e)
        {
            this.OnSizeChanged(e);
        }

        // Token: 0x060000B3 RID: 179 RVA: 0x00004704 File Offset: 0x00002904
        private void Control_MouseHover(object sender, EventArgs e)
        {
            this.OnMouseHover(e);
        }

        // Token: 0x060000B4 RID: 180 RVA: 0x0000470D File Offset: 0x0000290D
        private void Control_MouseLeave(object sender, EventArgs e)
        {
            this.OnMouseLeave(e);
        }

        // Token: 0x060000B5 RID: 181 RVA: 0x00004716 File Offset: 0x00002916
        private void Control_MouseEnter(object sender, EventArgs e)
        {
            this.OnMouseEnter(e);
        }

        // Token: 0x060000B6 RID: 182 RVA: 0x0000471F File Offset: 0x0000291F
        private void Control_LostFocus(object sender, EventArgs e)
        {
            base.InvokeLostFocus(this, EventArgs.Empty);
        }

        // Token: 0x060000B7 RID: 183 RVA: 0x0000472D File Offset: 0x0000292D
        private void Control_GotFocus(object sender, EventArgs e)
        {
            base.InvokeGotFocus(this, EventArgs.Empty);
        }

        /// <summary>
        /// Clean up event processing.
        /// </summary>
        /// <param name="e"></param>
        // Token: 0x060000B8 RID: 184 RVA: 0x0000473C File Offset: 0x0000293C
        protected override void OnControlRemoved(ControlEventArgs e)
        {
            if (!HSmartWindowControl.RunningInDesignerMode)
            {
                e.Control.Click -= this.ctrl_Click;
                e.Control.GotFocus -= this.Control_GotFocus;
                e.Control.LostFocus -= this.Control_LostFocus;
                e.Control.MouseEnter -= this.Control_MouseEnter;
                e.Control.MouseLeave -= this.Control_MouseLeave;
                e.Control.MouseHover -= this.Control_MouseHover;
                e.Control.SizeChanged -= this.Control_SizeChanged;
                e.Control.KeyDown -= this.Control_KeyDown;
                e.Control.KeyPress -= this.Control_KeyPress;
                e.Control.KeyUp -= this.Control_KeyUp;
                e.Control.Resize -= this.Control_Resize;
            }
            base.OnControlRemoved(e);
        }

        // Token: 0x060000B9 RID: 185 RVA: 0x00004858 File Offset: 0x00002A58
        private void CreateHWindow()
        {
            if (!HSmartWindowControl.RunningInDesignerMode)
            {
                this._hwindow = new HWindow(0, 0, base.Width, base.Height, "", "buffer", "");
                this._hwindow.SetPart(this._part.Top, this._part.Left, this._part.Top + this._part.Height - 1, this._part.Left + this._part.Width - 1);
                this._hwindow.SetWindowParam("graphics_stack", "true");
                this._prevsize.Width = base.Width;
                this._prevsize.Height = base.Height;
                this._dump_params = new HTuple(this._hwindow);
                this._dump_params[1] = "interleaved";
                this._hwindow.OnContentUpdate(new HWindow.ContentUpdateCallback(this.HWindowCallback));
                base.SizeChanged += this.HSmartWindowControl_SizeChanged;
                if (this.HInitWindow != null)
                {
                    this.HInitWindow(this, new EventArgs());
                }
            }
        }

        // Token: 0x060000BA RID: 186 RVA: 0x00004990 File Offset: 0x00002B90
        private void HSmartWindowControl_SizeChanged(object sender, EventArgs e)
        {
            this.WindowFrame.Size = base.Size;
            if (this.HKeepAspectRatio)
            {
                using (HTuple htuple = new HTuple(this._hwindow))
                {
                    this.calculate_part(htuple, this._prevsize.Width, this._prevsize.Height);
                }
            }
        }

        // Token: 0x060000BB RID: 187 RVA: 0x00004A08 File Offset: 0x00002C08
        private void HSmartWindowControl_Load(object sender, EventArgs e)
        {
            if (this._hwindow == null)
            {
                this.CreateHWindow();
            }
        }

        /// <summary>
        ///   Size of the HALCON window in pixels.
        /// </summary>
        // Token: 0x17000020 RID: 32
        // (get) Token: 0x060000BC RID: 188 RVA: 0x00004A18 File Offset: 0x00002C18
        // (set) Token: 0x060000BD RID: 189 RVA: 0x00004A55 File Offset: 0x00002C55
        [Category("Layout")]
        [Description("Size of the HALCON window in pixels.")]
        public Size WindowSize
        {
            get
            {
                if (HSmartWindowControl.RunningInDesignerMode || this._hwindow == null)
                {
                    return base.Size;
                }
                int num;
                int num2;
                int width;
                int height;
                this._hwindow.GetWindowExtents(out num, out num2, out width, out height);
                return new Size(width, height);
            }
            set
            {
                if (value.Width > 0 && value.Height > 0)
                {
                    base.Size = new Size(value.Width, value.Height);
                }
            }
        }

        // Token: 0x060000BE RID: 190 RVA: 0x00004A84 File Offset: 0x00002C84
        private void GetFloatPart(HWindow window, out double l1, out double c1, out double l2, out double c2)
        {
            HTuple t;
            HTuple t2;
            HTuple t3;
            HTuple t4;
            window.GetPart(out t, out t2, out t3, out t4);
            l1 = t;
            c1 = t2;
            l2 = t3;
            c2 = t4;
        }

        /// <summary>
        ///   Initial part of the HALCON window.
        /// </summary>
        // Token: 0x17000021 RID: 33
        // (get) Token: 0x060000BF RID: 191 RVA: 0x00004AC4 File Offset: 0x00002CC4
        // (set) Token: 0x060000C0 RID: 192 RVA: 0x00004B04 File Offset: 0x00002D04
        [Category("Layout")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("Visible image part (Column, Row, Width, Height).")]
        public Rectangle HImagePart
        {
            get
            {
                if (this._hwindow != null)
                {
                    int num;
                    int num2;
                    int num3;
                    int num4;
                    this._hwindow.GetPart(out num, out num2, out num3, out num4);
                    return new Rectangle(num2, num, num4 - num2 + 1, num3 - num + 1);
                }
                return this._part;
            }
            set
            {
                if (HSmartWindowControl.RunningInDesignerMode)
                {
                    this._part = value;
                    return;
                }
                if (value.Right <= 0 || value.Width <= 0)
                {
                    return;
                }
                if (this._hwindow != null)
                {
                    try
                    {
                        this._hwindow.SetPart(value.Top, value.Left, value.Top + value.Height - 1, value.Left + value.Width - 1);
                        this._part = value;
                        return;
                    }
                    catch (HalconException)
                    {
                        return;
                    }
                }
                this._part = value;
            }
        }

        // Token: 0x17000022 RID: 34
        // (get) Token: 0x060000C1 RID: 193 RVA: 0x00004B9C File Offset: 0x00002D9C
        // (set) Token: 0x060000C2 RID: 194 RVA: 0x00004BA4 File Offset: 0x00002DA4
        [Category("Behavior")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("Modifier key to interact with drawing objects. If a modifier key is selected, the user can only interact with drawing objects while keeping the modifier key pressed. This is especially useful when interacting with XLD drawing objects.")]
        public HSmartWindowControl.DrawingObjectsModifier HDrawingObjectsModifier
        {
            get
            {
                return this._drawingObjectsModifier;
            }
            set
            {
                this._drawingObjectsModifier = value;
            }
        }

        // Token: 0x17000023 RID: 35
        // (get) Token: 0x060000C3 RID: 195 RVA: 0x00004BAD File Offset: 0x00002DAD
        // (set) Token: 0x060000C4 RID: 196 RVA: 0x00004BB5 File Offset: 0x00002DB5
        [Category("Behavior")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("If on, the content of the HSmartWindowControl is moved when the mouse pointer is dragged.")]
        public bool HMoveContent
        {
            get
            {
                return this._automove;
            }
            set
            {
                this._automove = value;
            }
        }

        // Token: 0x17000024 RID: 36
        // (get) Token: 0x060000C5 RID: 197 RVA: 0x00004BBE File Offset: 0x00002DBE
        // (set) Token: 0x060000C6 RID: 198 RVA: 0x00004BC6 File Offset: 0x00002DC6
        [Category("Behavior")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("If on, the content of the HSmartWindowControl keeps its aspect ratio when the control is resized or zoomed.")]
        public bool HKeepAspectRatio
        {
            get
            {
                return this._keepaspectratio;
            }
            set
            {
                this._keepaspectratio = value;
            }
        }

        // Token: 0x17000025 RID: 37
        // (get) Token: 0x060000C7 RID: 199 RVA: 0x00004BCF File Offset: 0x00002DCF
        // (set) Token: 0x060000C8 RID: 200 RVA: 0x00004BD7 File Offset: 0x00002DD7
        [Category("Behavior")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("Controls the behavior of the mouse wheel.")]
        public HSmartWindowControl.ZoomContent HZoomContent
        {
            get
            {
                return this._zooming;
            }
            set
            {
                this._zooming = value;
            }
        }

        // Token: 0x17000026 RID: 38
        // (get) Token: 0x060000C9 RID: 201 RVA: 0x00004BE0 File Offset: 0x00002DE0
        // (set) Token: 0x060000CA RID: 202 RVA: 0x00004BE8 File Offset: 0x00002DE8
        [Category("Behavior")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("If on, double clicking resizes the content of the HSmartWindowControl to fit the size of the control. ")]
        public bool HDoubleClickToFitContent
        {
            get
            {
                return this._resetpart;
            }
            set
            {
                this._resetpart = value;
            }
        }

        /// <summary>
        /// Utility method that converts HALCON images into System.Drawing.Image used to
        /// display images in Window Controls.
        /// </summary>
        /// <param name="himage"></param>
        /// <returns></returns>
        // Token: 0x060000CB RID: 203 RVA: 0x00004BF4 File Offset: 0x00002DF4
        public static Image HalconToWinFormsImage(HImage himage)
        {
            HImage himage2 = himage.InterleaveChannels("argb", "match", 255);
            string text;
            int num;
            int height;
            IntPtr imagePointer = himage2.GetImagePointer1(out text, out num, out height);
            Bitmap bitmap = new Bitmap(num / 4, height, num, PixelFormat.Format32bppPArgb, imagePointer);
            Image result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                bitmap.Save(memoryStream, ImageFormat.Bmp);
                memoryStream.Position = 0L;
                result = Image.FromStream(memoryStream);
            }
            himage2.Dispose();
            bitmap.Dispose();
            return result;
        }

        /// <summary>
        /// In order to allow interaction with drawing objects and being able to
        /// zoom and drag the contents of the window, we need a mechanism to let
        /// the user decide what to do. Currently, if the user keeps the left
        /// Shift key pressed, he will be able to interact with the drawing
        /// objects. Otherwise, he works in the default modus.
        /// </summary>
        /// <returns>True if the user is currently pressing the left shift key.
        /// </returns>
        // Token: 0x060000CC RID: 204 RVA: 0x00004C8C File Offset: 0x00002E8C
        private bool InteractingWithDrawingObjs()
        {
            switch (this.HDrawingObjectsModifier)
            {
                case HSmartWindowControl.DrawingObjectsModifier.Shift:
                    return Control.ModifierKeys == Keys.Shift;
                case HSmartWindowControl.DrawingObjectsModifier.Ctrl:
                    return Control.ModifierKeys == Keys.Control;
                case HSmartWindowControl.DrawingObjectsModifier.Alt:
                    return Control.ModifierKeys == Keys.Alt;
                default:
                    return true;
            }
        }

        /// <summary>
        /// Translates native encoding of mouse buttons to HALCON encoding
        /// (see get_mposition).
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        // Token: 0x060000CD RID: 205 RVA: 0x00004CDE File Offset: 0x00002EDE
        private int MouseEventToInt(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                return 1;
            }
            if (e.Button == MouseButtons.Right)
            {
                return 4;
            }
            if (e.Button == MouseButtons.Middle)
            {
                return 2;
            }
            return 0;
        }

        // Token: 0x060000CE RID: 206 RVA: 0x00004D10 File Offset: 0x00002F10
        private HMouseEventArgs ToHMouse(MouseEventArgs e)
        {
            double y;
            double x;
            this._hwindow.ConvertCoordinatesWindowToImage((double)e.Y, (double)e.X, out y, out x);
            return new HMouseEventArgs(e.Button, e.Clicks, x, y, e.Delta);
        }

        // Token: 0x060000CF RID: 207 RVA: 0x00004D54 File Offset: 0x00002F54
        private void WindowFrame_MouseDown(object sender, MouseEventArgs e)
        {
            HMouseEventArgs e2 = null;
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    this._left_button_down = true;
                    this._last_position.X = e.X;
                    this._last_position.Y = e.Y;
                }
                if (this.InteractingWithDrawingObjs())
                {
                    double d;
                    double d2;
                    this._hwindow.ConvertCoordinatesWindowToImage((double)e.Y, (double)e.X, out d, out d2);
                    this._hwindow.SendMouseDownEvent(d, d2, this.MouseEventToInt(e));
                }
                e2 = this.ToHMouse(e);
            }
            catch (HalconException he)
            {
                if (this.HErrorNotify != null)
                {
                    this.HErrorNotify(he);
                }
            }
            if (this.HMouseDown != null)
            {
                this.HMouseDown(this, e2);
            }
        }

        /// <summary>
        /// Shifts the window contents by (dx, dy) pixels.
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        // Token: 0x060000D0 RID: 208 RVA: 0x00004E24 File Offset: 0x00003024
        public void HShiftWindowContents(double dx, double dy)
        {
            double num;
            double num2;
            double num3;
            double num4;
            this.GetFloatPart(this._hwindow, out num, out num2, out num3, out num4);
            int num5;
            int num6;
            int num7;
            int num8;
            this._hwindow.GetWindowExtents(out num5, out num6, out num7, out num8);
            double num9 = (num4 - num2 + 1.0) / (double)num7;
            double num10 = (num3 - num + 1.0) / (double)num8;
            try
            {
                this._hwindow.SetPart(num + dy * num10, num2 + dx * num9, num3 + dy * num10, num4 + dx * num9);
            }
            catch (HalconException)
            {
            }
        }

        // Token: 0x060000D1 RID: 209 RVA: 0x00004ECC File Offset: 0x000030CC
        private void WindowFrame_MouseMove(object sender, MouseEventArgs e)
        {
            HMouseEventArgs e2 = null;
            try
            {
                bool flag = false;
                if (this._left_button_down && this.InteractingWithDrawingObjs())
                {
                    double d;
                    double d2;
                    this._hwindow.ConvertCoordinatesWindowToImage((double)e.Y, (double)e.X, out d, out d2);
                    //flag = this._hwindow.SendMouseDragEvent(d, d2, this.MouseEventToInt(e))[0].S.Equals("true");
                    flag = this._hwindow.SendMouseDragEvent(d, d2, this.MouseEventToInt(e)).Equals("true");
                }
                if (!flag && this._left_button_down && this.HMoveContent)
                {
                    this.HShiftWindowContents((double)(this._last_position.X - e.X), (double)(this._last_position.Y - e.Y));
                }
                this._last_position.X = e.X;
                this._last_position.Y = e.Y;
                e2 = this.ToHMouse(e);
            }
            catch (HalconException he)
            {
                if (this.HErrorNotify != null)
                {
                    this.HErrorNotify(he);
                }
            }
            if (this.HMouseMove != null)
            {
                this.HMouseMove(this, e2);
            }
        }

        // Token: 0x060000D2 RID: 210 RVA: 0x00004FEC File Offset: 0x000031EC
        private void WindowFrame_MouseUp(object sender, MouseEventArgs e)
        {
            HMouseEventArgs e2 = null;
            try
            {
                double d;
                double d2;
                this._hwindow.ConvertCoordinatesWindowToImage((double)e.Y, (double)e.X, out d, out d2);
                this._hwindow.SendMouseUpEvent(d, d2, this.MouseEventToInt(e));
                if (e.Button == MouseButtons.Left)
                {
                    this._left_button_down = false;
                }
                this._last_position.X = e.X;
                this._last_position.Y = e.Y;
                e2 = this.ToHMouse(e);
            }
            catch (HalconException he)
            {
                if (this.HErrorNotify != null)
                {
                    this.HErrorNotify(he);
                }
            }
            if (this.HMouseUp != null)
            {
                this.HMouseUp(this, e2);
            }
        }

        // Token: 0x060000D3 RID: 211 RVA: 0x000050B4 File Offset: 0x000032B4
        private void WindowFrame_DoubleClick(object sender, EventArgs e)
        {
            HMouseEventArgs e2 = null;
            try
            {
                bool flag = false;
                MouseEventArgs mouseEventArgs = (MouseEventArgs)e;
                this._last_position.X = mouseEventArgs.X;
                this._last_position.Y = mouseEventArgs.Y;
                if (mouseEventArgs.Button == MouseButtons.Left && this.InteractingWithDrawingObjs())
                {
                    double d;
                    double d2;
                    this._hwindow.ConvertCoordinatesWindowToImage((double)this._last_position.Y, (double)this._last_position.X, out d, out d2);
                    //flag = this._hwindow.SendMouseDoubleClickEvent(d, d2, this.MouseEventToInt(mouseEventArgs))[0].S.Equals("true");
                    flag = this._hwindow.SendMouseDoubleClickEvent(d, d2, this.MouseEventToInt(mouseEventArgs)).Equals("true");
                }
                if (!flag && this.HDoubleClickToFitContent)
                {
                    this.SetFullImagePart(null);
                }
                e2 = this.ToHMouse(mouseEventArgs);
            }
            catch (HalconException he)
            {
                if (this.HErrorNotify != null)
                {
                    this.HErrorNotify(he);
                }
            }
            if (this.HMouseDoubleClick != null)
            {
                this.HMouseDoubleClick(this, e2);
            }
        }

        // Token: 0x060000D4 RID: 212 RVA: 0x000051C0 File Offset: 0x000033C0
        private void WindowFrame_MouseLeave(object sender, EventArgs e)
        {
            this._left_button_down = false;
        }

        /// <summary>
        /// UserControls under Windows Forms do not support the mouse wheel event.
        /// As a solution, the user can set his MouseWheel event in his form to
        /// call this method.
        /// Please notice that the Visual Studio Designer does not show this event.
        /// The reason is that UserControls do not support this type of event.
        /// Hence, you need to manually add it to the initialization code of your
        /// Windows Form, and set it to call the HSmartWindowControl_MouseWheel
        /// method of the HALCON Window Control.
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        // Token: 0x060000D5 RID: 213 RVA: 0x000051CC File Offset: 0x000033CC
        public void HSmartWindowControl_MouseWheel(object sender, MouseEventArgs e)
        {
            HMouseEventArgs e2 = null;
            try
            {
                if (this._zooming != HSmartWindowControl.ZoomContent.Off)
                {
                    HTuple homMat2D;
                    HOperatorSet.HomMat2dIdentity(out homMat2D);
                    Point point = base.PointToClient(Cursor.Position);
                    double d;
                    double d2;
                    this._hwindow.ConvertCoordinatesWindowToImage((double)point.Y, (double)point.X, out d, out d2);
                    double num = (e.Delta < 0) ? Math.Sqrt(2.0) : (1.0 / Math.Sqrt(2.0));
                    if (this.HZoomContent == HSmartWindowControl.ZoomContent.WheelBackwardZoomsIn)
                    {
                        num = 1.0 / num;
                    }
                    for (int i = Math.Abs(e.Delta) / 120; i > 1; i--)
                    {
                        num *= ((e.Delta < 0) ? Math.Sqrt(2.0) : (1.0 / Math.Sqrt(2.0)));
                    }
                    HTuple homMat2D2;
                    HOperatorSet.HomMat2dScale(homMat2D, num, num, d2, d, out homMat2D2);
                    double d3;
                    double d4;
                    double d5;
                    double d6;
                    this.GetFloatPart(this._hwindow, out d3, out d4, out d5, out d6);
                    HTuple htuple;
                    HTuple htuple2;
                    HOperatorSet.AffineTransPoint2d(homMat2D2, d4, d3, out htuple, out htuple2);
                    HTuple htuple3;
                    HTuple htuple4;
                    HOperatorSet.AffineTransPoint2d(homMat2D2, d6, d5, out htuple3, out htuple4);
                    e2 = this.ToHMouse(e);
                    try
                    {
                        this._hwindow.SetPart(htuple2.D, htuple.D, htuple4.D, htuple3.D);
                        goto IL_1BB;
                    }
                    catch (Exception)
                    {
                        this._hwindow.SetPart(d3, d4, d5, d6);
                        goto IL_1BB;
                    }
                }
                e2 = this.ToHMouse(e);
            IL_1BB:;
            }
            catch (HalconException he)
            {
                if (this.HErrorNotify != null)
                {
                    this.HErrorNotify(he);
                }
            }
            if (this.HMouseWheel != null)
            {
                this.HMouseWheel(this, e2);
            }
        }

        // Token: 0x060000D6 RID: 214 RVA: 0x000053F8 File Offset: 0x000035F8
        private void HSmartWindowControl_Paint(object sender, PaintEventArgs e)
        {
            if (this._hwindow == null || HSmartWindowControl.RunningInDesignerMode)
            {
                return;
            }
            bool flag = false;
            int num;
            int num2;
            int num3;
            int num4;
            this._hwindow.GetWindowExtents(out num, out num2, out num3, out num4);
            if (base.Width > 0 && base.Height > 0 && (num3 != base.Width || num4 != base.Height))
            {
                this.WindowFrame.Width = base.Width;
                this.WindowFrame.Height = base.Height;
                int num5;
                int num6;
                int width;
                int height;
                this._hwindow.GetWindowExtents(out num5, out num6, out width, out height);
                try
                {
                    this._hwindow.SetWindowExtents(0, 0, base.Width, base.Height);
                    flag = true;
                }
                catch (HalconException)
                {
                    this._hwindow.SetWindowExtents(0, 0, width, height);
                }
            }
            if (this.HKeepAspectRatio && flag)
            {
                using (HTuple htuple = new HTuple(this._hwindow))
                {
                    this.calculate_part(htuple, this._prevsize.Width, this._prevsize.Height);
                }
            }
            this._prevsize.Width = this.WindowFrame.Width;
            this._prevsize.Height = this.WindowFrame.Height;
            this._netimg.Dispose();
            HOperatorSet.DumpWindowImage(out this._netimg, this._dump_params);
            HTuple htuple2;
            HTuple htuple3;
            HTuple htuple4;
            HTuple t;
            HOperatorSet.GetImagePointer1(this._netimg, out htuple2, out htuple3, out htuple4, out t);
            Bitmap image = new Bitmap(htuple4 / 4, t, htuple4, PixelFormat.Format32bppPArgb, (IntPtr)htuple2.L);
            this.WindowFrame.Image = image;
        }

        // Token: 0x060000D7 RID: 215 RVA: 0x000055C0 File Offset: 0x000037C0
        private bool calculate_part(HTuple hv_WindowHandle, HTuple hv_WindowWidth, HTuple hv_WindowHeight)
        {
            HTuple htuple = null;
            HTuple htuple2 = null;
            HTuple htuple3 = null;
            HTuple htuple4 = null;
            HTuple htuple5 = null;
            HTuple htuple6 = null;
            HTuple t = null;
            HTuple t2 = null;
            HTuple homMat2D = null;
            HTuple homMat2D2 = null;
            HTuple htuple7 = null;
            HTuple htuple8 = null;
            bool result = true;
            HOperatorSet.GetPart(hv_WindowHandle, out htuple, out htuple2, out htuple3, out htuple4);
            try
            {
                HTuple t3 = htuple4 - htuple2 + 1;
                HTuple htuple9 = htuple3 - htuple + 1;
                //t3 / htuple9.TupleReal();
                //t3 / htuple9.TupleReal();
                HOperatorSet.GetWindowExtents(hv_WindowHandle, out htuple5, out htuple6, out t, out t2);
                HTuple sx = t / hv_WindowWidth.TupleReal();
                HTuple sy = t2 / hv_WindowHeight.TupleReal();
                HTuple htuple10 = new HTuple();
                htuple10 = htuple10.TupleConcat((htuple + htuple3) * 0.5);
                htuple10 = htuple10.TupleConcat((htuple2 + htuple4) * 0.5);
                HOperatorSet.HomMat2dIdentity(out homMat2D);
                HOperatorSet.HomMat2dScale(homMat2D, sx, sy, htuple10.TupleSelect(1), htuple10.TupleSelect(0), out homMat2D2);
                HOperatorSet.AffineTransPoint2d(homMat2D2, htuple2.TupleConcat(htuple4), htuple.TupleConcat(htuple3), out htuple7, out htuple8);
                HOperatorSet.SetPart(hv_WindowHandle, htuple8.TupleSelect(0), htuple7.TupleSelect(0), htuple8.TupleSelect(1), htuple7.TupleSelect(1));
            }
            catch (HalconException)
            {
                HOperatorSet.SetPart(hv_WindowHandle, htuple, htuple2, htuple3, htuple4);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        // Token: 0x060000D8 RID: 216 RVA: 0x00005754 File Offset: 0x00003954
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this._hwindow != null)
                {
                    this._hwindow.Dispose();
                }
                if (this._dump_params != null)
                {
                    this._dump_params.Dispose();
                }
                if (this.components != null)
                {
                    this.components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        // Token: 0x060000D9 RID: 217 RVA: 0x000057A4 File Offset: 0x000039A4
        private void InitializeComponent()
        {
            this.WindowFrame = new PictureBox();
            ((ISupportInitialize)this.WindowFrame).BeginInit();
            base.SuspendLayout();
            this.WindowFrame.BackColor = SystemColors.Desktop;
            this.WindowFrame.Dock = DockStyle.Fill;
            this.WindowFrame.Location = new Point(0, 0);
            this.WindowFrame.Margin = new Padding(0);
            this.WindowFrame.Name = "WindowFrame";
            this.WindowFrame.Size = new Size(512, 512);
            this.WindowFrame.TabIndex = 0;
            this.WindowFrame.TabStop = false;
            this.WindowFrame.MouseDoubleClick += new MouseEventHandler(this.WindowFrame_DoubleClick);
            this.WindowFrame.MouseDown += this.WindowFrame_MouseDown;
            this.WindowFrame.MouseMove += this.WindowFrame_MouseMove;
            this.WindowFrame.MouseUp += this.WindowFrame_MouseUp;
            this.WindowFrame.MouseLeave += this.WindowFrame_MouseLeave;
            base.AutoScaleMode = AutoScaleMode.None;
            base.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.AutoValidate = AutoValidate.EnableAllowFocusChange;
            base.Controls.Add(this.WindowFrame);
            base.Margin = new Padding(0);
            base.Name = "HSmartWindowControl";
            base.Size = new Size(512, 512);
            base.Load += this.HSmartWindowControl_Load;
            base.Paint += this.HSmartWindowControl_Paint;
            ((ISupportInitialize)this.WindowFrame).EndInit();
            base.ResumeLayout(false);
        }

        // Token: 0x04000057 RID: 87
        private const string positionDescription = " The position is returned in the image coordinate system.";

        // Token: 0x0400005F RID: 95
        private HWindow _hwindow;

        // Token: 0x04000060 RID: 96
        private Point _last_position = new Point(0, 0);

        // Token: 0x04000061 RID: 97
        private HObject _netimg = new HObject();

        // Token: 0x04000062 RID: 98
        private Size _prevsize;

        // Token: 0x04000063 RID: 99
        private HTuple _dump_params;

        // Token: 0x04000064 RID: 100
        private bool _left_button_down;

        // Token: 0x04000065 RID: 101
        private Rectangle _part = new Rectangle(0, 0, 640, 480);

        // Token: 0x04000066 RID: 102
        private HSmartWindowControl.DrawingObjectsModifier _drawingObjectsModifier;

        // Token: 0x04000067 RID: 103
        private bool _automove = true;

        // Token: 0x04000068 RID: 104
        private bool _keepaspectratio = true;

        // Token: 0x04000069 RID: 105
        private HSmartWindowControl.ZoomContent _zooming = HSmartWindowControl.ZoomContent.WheelForwardZoomsIn;

        // Token: 0x0400006A RID: 106
        private bool _resetpart = true;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        // Token: 0x0400006B RID: 107
        private IContainer components;

        // Token: 0x0400006C RID: 108
        private PictureBox WindowFrame;

        /// <summary>
        /// In some situations (like a missing license in runtime), it can be the case that
        /// internal exceptions are thrown, and the user has no way of capturing them.
        /// This callback allows the user to react to such runtime errors.
        /// </summary>
        /// <param name="he"></param>
        // Token: 0x020000A2 RID: 162
        // (Invoke) Token: 0x06002032 RID: 8242
        public delegate void HErrorHandler(HalconException he);

        /// <summary>
        /// Modifier to manipulate drawing objects
        /// </summary>
        // Token: 0x020000A3 RID: 163
        public enum DrawingObjectsModifier
        {
            /// <summary>
            /// Manipulate drawing objects without a modifier
            /// </summary>
            // Token: 0x04001191 RID: 4497
            None,
            /// <summary>
            /// Shift key must be pressed to modify drawing objects
            /// </summary>
            // Token: 0x04001192 RID: 4498
            Shift,
            /// <summary>
            /// Ctrl key must be pressed to modify drawing objects
            /// </summary>
            // Token: 0x04001193 RID: 4499
            Ctrl,
            /// <summary>
            /// Alt key must be pressed to modify drawing objects
            /// </summary>
            // Token: 0x04001194 RID: 4500
            Alt
        }

        /// <summary>
        /// Mouse wheel behavior
        /// </summary>
        // Token: 0x020000A4 RID: 164
        public enum ZoomContent
        {
            /// <summary>
            /// No effect on the contents
            /// </summary>
            // Token: 0x04001196 RID: 4502
            Off,
            /// <summary>
            /// Moving the mouse wheel forward zooms in on the contents
            /// </summary>
            // Token: 0x04001197 RID: 4503
            WheelForwardZoomsIn,
            /// <summary>
            /// Moving the mouse wheel backward zooms in on the contents
            /// </summary>
            // Token: 0x04001198 RID: 4504
            WheelBackwardZoomsIn
        }
    }
}
